//
//  Utils.m
//  SSWiFi
//
//  Created by 酷安 @啥也不会啊啊啊啊啊啊  on 2024/3/10.
//

#import "Utils.h"
#import "Api.h"

@implementation Utils
+ (void)testLoginIp:(NSArray *)loginIps loginPaths:(NSArray *)loginPaths CallBack:(void (^)(NSArray * _Nonnull))callback{
    dispatch_group_t apiGroup = dispatch_group_create();
    dispatch_queue_t concurrentQueue = dispatch_queue_create("com.example.concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    NSMutableArray *successLogins = [NSMutableArray array];
    for(NSDictionary *i in loginIps){
        for(NSDictionary *p in loginPaths ){
            NSString *ip = [i objectForKey:@"ip"];
            NSString *loginUrl = [NSString stringWithFormat:@"http://%@/%@",ip,[p objectForKey:@"set"]];
            NSString *pwd = [i objectForKey:@"pwd"];
            Api *api = [[Api alloc] init];
            dispatch_group_enter(apiGroup);
            [api testLogin:loginUrl pwd:pwd callback:^(bool flag) {
                if(flag){
                    [successLogins addObject:@{
                        @"ip":ip,
                        @"pwd":pwd,
                        @"path":p,
                    }];
                }
                dispatch_group_leave(apiGroup);
            }];
        }
    }
    dispatch_group_notify(apiGroup, concurrentQueue, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            !callback ? :callback(successLogins);
        });
    });
}

+ (NSString *)appVersion{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}




+ (void)alertTextDig:(NSString *)title placehold:(NSString *)placehold text:(NSString *)text keyboardType:(UIKeyboardType)keyboardType enablesSubmitButtonAutomatically:(Boolean)enablesSubmitButtonAutomatically block:(void (^)(NSString *value, QMUIDialogViewController *aDialogViewController))block{
    QMUIDialogTextFieldViewController *dialogViewController = [[QMUIDialogTextFieldViewController alloc] init];
    dialogViewController.title = title;
    [dialogViewController addTextFieldWithTitle:nil configurationHandler:^(QMUILabel *titleLabel, QMUITextField *textField, CALayer *separatorLayer) {
        textField.placeholder = placehold;
        textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        textField.autocorrectionType = UITextAutocorrectionTypeNo;
        if(text.length > 0){
            textField.text = text;
        }
        textField.keyboardType = keyboardType;
//        textField.maximumTextLength = 10;
    }];
    dialogViewController.enablesSubmitButtonAutomatically = enablesSubmitButtonAutomatically;
    [dialogViewController addCancelButtonWithText:@"取消" block:nil];
    [dialogViewController addSubmitButtonWithText:@"确定" block:^(QMUIDialogTextFieldViewController *aDialogViewController) {
        NSString *value = [aDialogViewController.textFields.firstObject.text qmui_trim];
        !block ? : block(value,aDialogViewController);
    }];
    [dialogViewController show];
}


+ (NSString *)loadSpeedUrl{
    NSString *speedurl = [[NSUserDefaults standardUserDefaults] valueForKey:@"speedurl"];
    return speedurl;
}

+ (void)saveSpeedUrl:(NSString *)speedurl{
    [[NSUserDefaults standardUserDefaults] setValue:speedurl forKey:@"speedurl"];
}
@end
